<!doctype html>
<html lang="zh-CN">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title><?= htmlspecialchars($title ?? 'MyCMS 管理后台') ?></title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, Segoe UI, Roboto, Helvetica, Arial, sans-serif;
            line-height: 1.6;
            color: #333;
            background: #f8f9fa;
            display: flex;
            min-height: 100vh;
        }

        .sidebar {
            width: 250px;
            background: #343a40;
            color: #fff;
            position: fixed;
            height: 100vh;
            overflow-y: auto;
        }

        .sidebar-header {
            padding: 1.5rem;
            border-bottom: 1px solid #495057;
        }

        .sidebar-brand {
            font-size: 1.25rem;
            font-weight: bold;
            color: #fff;
            text-decoration: none;
            display: flex;
            align-items: center;
            gap: 0.5rem;
        }

        .sidebar-brand:hover {
            color: #f8f9fa;
        }

        .sidebar-nav {
            padding: 1rem 0;
        }

        .sidebar-nav-item {
            display: block;
            padding: 0.75rem 1.5rem;
            color: rgba(255, 255, 255, 0.8);
            text-decoration: none;
            transition: all 0.2s;
            border-left: 3px solid transparent;
        }

        .sidebar-nav-item:hover,
        .sidebar-nav-item.active {
            background: rgba(255, 255, 255, 0.1);
            color: #fff;
            border-left-color: #007bff;
        }

        .sidebar-divider {
            height: 1px;
            background: #495057;
            margin: 1rem 0;
        }

        .sidebar-footer {
            padding: 1rem 1.5rem;
            font-size: 0.8rem;
            color: rgba(255, 255, 255, 0.5);
            border-top: 1px solid #495057;
        }

        .main-content {
            flex: 1;
            margin-left: 250px;
            padding: 2rem;
        }

        .header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 2rem;
            padding-bottom: 1rem;
            border-bottom: 1px solid #e9ecef;
        }

        .header-title {
            font-size: 1.5rem;
            font-weight: bold;
            color: #343a40;
        }

        .header-actions {
            display: flex;
            gap: 1rem;
            align-items: center;
        }

        .user-dropdown {
            position: relative;
        }

        .user-info {
            display: flex;
            align-items: center;
            gap: 0.5rem;
            cursor: pointer;
            padding: 0.5rem;
            border-radius: 4px;
        }

        .user-info:hover {
            background: #e9ecef;
        }

        .alert {
            padding: 1rem;
            margin-bottom: 1.5rem;
            border-radius: 4px;
            border: 1px solid transparent;
        }

        .alert-success {
            color: #155724;
            background-color: #d4edda;
            border-color: #c3e6cb;
        }

        .alert-error {
            color: #721c24;
            background-color: #f8d7da;
            border-color: #f5c6cb;
        }

        .alert-warning {
            color: #856404;
            background-color: #fff3cd;
            border-color: #ffeaa7;
        }

        .alert-info {
            color: #0c5460;
            background-color: #d1ecf1;
            border-color: #bee5eb;
        }

        .card {
            background: #fff;
            border-radius: 4px;
            box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
            margin-bottom: 1.5rem;
        }

        .card-header {
            padding: 1rem 1.5rem;
            border-bottom: 1px solid #e9ecef;
            font-weight: bold;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .card-body {
            padding: 1.5rem;
        }

        .btn {
            display: inline-block;
            padding: 0.6rem 1.2rem;
            background: #007bff;
            color: white;
            text-decoration: none;
            border-radius: 4px;
            border: none;
            cursor: pointer;
            font-size: 0.9rem;
            transition: background 0.2s;
        }

        .btn:hover {
            background: #0056b3;
        }

        .btn-secondary {
            background: #6c757d;
        }

        .btn-secondary:hover {
            background: #545b62;
        }

        .btn-danger {
            background: #dc3545;
        }

        .btn-danger:hover {
            background: #c82333;
        }

        .btn-sm {
            padding: 0.4rem 0.8rem;
            font-size: 0.8rem;
        }

        .table {
            width: 100%;
            border-collapse: collapse;
        }

        .table th,
        .table td {
            padding: 0.75rem;
            border-bottom: 1px solid #e9ecef;
            text-align: left;
        }

        .table th {
            background: #f8f9fa;
            font-weight: bold;
        }

        .table tr:hover {
            background: #f8f9fa;
        }

        .form-group {
            margin-bottom: 1.5rem;
        }

        .form-label {
            display: block;
            margin-bottom: 0.5rem;
            font-weight: bold;
        }

        .form-control {
            width: 100%;
            padding: 0.75rem;
            border: 1px solid #ced4da;
            border-radius: 4px;
            font-size: 1rem;
        }

        .form-control:focus {
            border-color: #80bdff;
            outline: 0;
            box-shadow: 0 0 0 0.2rem rgba(0, 123, 255, 0.25);
        }

        .form-text {
            margin-top: 0.25rem;
            font-size: 0.8rem;
            color: #6c757d;
        }

        .badge {
            display: inline-block;
            padding: 0.25rem 0.5rem;
            font-size: 0.75rem;
            font-weight: bold;
            border-radius: 10rem;
            text-align: center;
            white-space: nowrap;
            vertical-align: baseline;
        }

        .badge-primary {
            background: #007bff;
            color: white;
        }

        .badge-secondary {
            background: #6c757d;
            color: white;
        }

        .badge-success {
            background: #28a745;
            color: white;
        }

        .badge-danger {
            background: #dc3545;
            color: white;
        }

        .badge-warning {
            background: #ffc107;
            color: #212529;
        }

        .badge-info {
            background: #17a2b8;
            color: white;
        }
    </style>
</head>

<body>
    <div class="sidebar">
        <div class="sidebar-header">
            <a href="/admin" class="sidebar-brand">
                MyCMS 管理后台
            </a>
        </div>
        <div class="sidebar-nav">
            <a href="/admin" class="sidebar-nav-item <?= strpos($_SERVER['REQUEST_URI'], '/admin') === 0 && $_SERVER['REQUEST_URI'] === '/admin' ? 'active' : '' ?>">
                仪表盘
            </a>
            <a href="/admin/posts" class="sidebar-nav-item <?= strpos($_SERVER['REQUEST_URI'], '/admin/posts') === 0 ? 'active' : '' ?>">
                文章管理
            </a>
            <a href="/admin/users" class="sidebar-nav-item <?= strpos($_SERVER['REQUEST_URI'], '/admin/users') === 0 ? 'active' : '' ?>">
                用户管理
            </a>
            <a href="/admin/logs" class="sidebar-nav-item <?= strpos($_SERVER['REQUEST_URI'], '/admin/logs') === 0 ? 'active' : '' ?>">
                系统日志
            </a>
            <a href="/admin/backup" class="sidebar-nav-item <?= strpos($_SERVER['REQUEST_URI'], '/admin/backup') === 0 ? 'active' : '' ?>">
                数据备份
            </a>
            <a href="/admin/settings" class="sidebar-nav-item <?= strpos($_SERVER['REQUEST_URI'], '/admin/settings') === 0 ? 'active' : '' ?>">
                系统设置
            </a>
            <a href="/admin/themes" class="sidebar-nav-item <?= strpos($_SERVER['REQUEST_URI'], '/admin/themes') === 0 ? 'active' : '' ?>">
                主题管理
            </a>

            <div class="sidebar-divider"></div>
            <a href="/" class="sidebar-nav-item">
                访问前台
            </a>
            <a href="/logout" class="sidebar-nav-item">
                退出登录
            </a>
        </div>
        <div class="sidebar-footer">
            MyCMS v1.0.0
        </div>
    </div>

    <div class="main-content">
        <div class="header">
            <div class="header-title">
                <?= htmlspecialchars($title ?? 'MyCMS 管理后台') ?>
            </div>
            <div class="header-actions">
                <?php
                // 获取当前用户信息
                $currentUser = null;
                try {
                    $currentUser = App\Services\Auth::user();
                } catch (Exception $e) {
                    // 忽略错误，用户未登录
                }
                ?>
                <div class="user-info">
                    <?= htmlspecialchars($currentUser['username'] ?? '管理员') ?>
                </div>
            </div>
        </div>

        <?php
        // 显示闪现消息
        $flashTypes = ['success', 'error', 'warning', 'info'];
        foreach ($flashTypes as $type) {
            try {
                if (App\Core\Flash::has($type)) {
                    $message = App\Core\Flash::get($type);
                    echo '<div class="alert alert-' . htmlspecialchars($type) . '">';
                    echo htmlspecialchars($message);
                    echo '</div>';
                }
            } catch (Exception $e) {
                // 忽略Flash消息错误
            }
        }
        ?>

        <?= $content ?? '' ?>
    </div>
</body>

</html>